var http = require('http');
var ip = "127.0.0.1";
var util = require('util');
var fs = require("fs");
var url = require("url");
var port = 80;
var siteName = "我的网站";

var querystring = require('querystring');
var mysql = require('mysql');
			var conn = mysql.createConnection({                
				host:'localhost',                          
				user:'root',         
				password:'123',                                                    
				database:'messageboard',                                 
				port:3306             
			}); 
conn.connect();

http.createServer(function (request, response) {
	response.statusCode=200;
	response.setHeader('Content-Type','text/html');	
	response.setHeader('Server','Private Server');

	
	var data ="404";
	var s ="";
	var p = url.parse(request.url,true);
if(p.pathname == '/hello.html'){
	try{
		
		data = fs.readFileSync("."+request.url,"utf-8")
		data = data.replace(/%title/g,siteName)
		/*item = fs.readFileSync("data.json","utf-8")
		item = JSON.parse(item);
*/
		 
			
			conn.query('select * from message', function(err, rows, fields) {
			    if (err) throw err;
			    console.log(rows);
			    item = rows;
			    for(var i = 0;i<item.length;i++){
					s+='<div class="item">'
					s+='<h6>'+item[i].author+'<span>'+item[i].date+'</span></h6>'
					s+='<p>'+item[i].message+'<a href="delmessage?id='+item[i].id+'">删除</a></p>'
					s+='</div>'
				}
				data = data.replace(/%main/g,s)

				response.write(data);
				response.end();
			});
		


		
	}catch(e){
     	
	}
	
	

}else if (p.pathname == '/form.html') {
	data = fs.readFileSync("."+request.url,"utf-8")
	response.write(data);
	response.end();

}else if(p.pathname == '/addmessage'){
	/*author=p.query.author;
	message=p.query.message;
	item =JSON.parse(fs.readFileSync("data.json","utf-8"))
	item.push({
		author:author,
		message:message,
		date:new Date()
	})

	item = JSON.stringify(item)
	item = fs.writeFileSync("data.json",item)
	data = "提交成功"
	response.writeHead(302,{
		'Location':'/hello.html'
	})*/

	var post = ''; 
	request.on('data', function(data){    
        post += data;
    });
	request.on('end', function(){
		post = querystring.parse(post);
		/*
		author=post.author;
		message=post.message;
		item =JSON.parse(fs.readFileSync("data.json","utf-8"))
		item.push({
			author:author,
			message:message,
			date:new Date()
		})
			item = JSON.stringify(item)
			item = fs.writeFileSync("data.json",item)
	       console.log(post)*/
	      
			conn.connect();
			var date = new Date();
			var d = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+' '+date.getHours()+":"+date.getMinutes();
			conn.query('insert into message values(null,"'+post.author+'","'+post.message+'","'+d+'")', function(err, rows, fields) {
			    if (err) throw err;
			    console.log(rows);
			});
		


	       	response.writeHead(302,{
			'Location':'/hello.html'
			})
			response.end();
	    }); 


		data = "提交成功"

}else if(p.pathname == '/delmessage'){
	id = p.query.id;
	/*item =JSON.parse(fs.readFileSync("data.json","utf-8"));
	item.splice(id,1);
	item = JSON.stringify(item);
	item = fs.writeFileSync("data.json",item);
	data = "提交成功"*/
	
	console.log("删除id:"+id)

	
	conn.query('delete from message where id='+id, function(err, rows, fields) {
	    if (err) throw err;   

		response.writeHead(302,{
		'Location':'/hello.html'
	});
		
		response.end();	
	});


	
	

}


	//data = util.format(data,siteName,siteName)
	
	
}).listen(port);

console.log('Server running at http://%s:%s/',ip,port);